/**
 *
 */
package com.freedom.study.util;

import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.ParseException;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;

/**
 * 描述：拿到es的客户端
 * 
 * @author LiPan
 * @since 2017年11月16日上午11:40:09
 */
public class EsClientUtil {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private static RestClient restClient = null;

	public static RestClient getEsClient() {
		restClient = RestClient.builder(new HttpHost(EsConstant.ES_ADRESS, EsConstant.ES_PORT, EsConstant.ES_METHOD))
				.build();
		return restClient;
	}

	// 关闭资源
	public static void closeEsClient(RestClient restClient) {
		if (null != restClient) {
			try {
				restClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 获取资源
	public static Map<String, Object> getEsData(String param1, String param2) {
		RestClient esClient = getEsClient();
		String id = createId(param1, param2);
		Response response = null;
		try {
			response = restClient.performRequest(EsConstant.ES_GET, EsConstant.ES_URL + id);
			Map<String, Object> parse = (Map<String, Object>) JSON.parse(EntityUtils.toString(response.getEntity()));
			return parse;
		} catch (ParseException e) {
			return null;
		} catch (IOException e) {
			return null;
		} finally {
			closeEsClient(esClient);
		}
	}

	// 新增资源
	public static boolean putEsData(String data, String param1, String param2) {
		HttpEntity entity = new NStringEntity(data, ContentType.APPLICATION_JSON);// 防止中文乱码,必须加ContentType.APPLICATION_JSON
		RestClient restClient = getEsClient();
		String id = createId(param1, param2);
		try {
			restClient.performRequest(EsConstant.ES_PUT, EsConstant.ES_URL + id,
					Collections.singletonMap("pretty", "true"), entity);
		} catch (IOException e) {
			System.err.println("存入数据失败....");
			e.printStackTrace();
			return false;
		} finally {
			closeEsClient(restClient);
		}

		return true;
	}

	// 删除
	public static boolean deleteEsData(String param1, String param2) {
		RestClient esClient = getEsClient();
		String id = createId(param1, param2);
		Response response = null;
		try {
			response = restClient.performRequest(EsConstant.ES_DELETE, EsConstant.ES_URL + id);
			return true;
		} catch (ParseException e) {
			return false;
		} catch (IOException e) {
			return false;
		} finally {
			closeEsClient(esClient);
		}
	}

	// 创建id
	public static String createId(String param1, String param2) {
		String id_first = new ChineseCharToEn().getAllFirstLetter(param1);
		String id_second = new ChineseCharToEn().getAllFirstLetter(param2) + id_first;
		return id_second;
	}

}
